﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using Acc.Lib;
using Data.UserDb;
using System.Text;
using ICanPay;
using ICanPay.Providers;

namespace Acc_Web
{
    public partial class Step3 : System.Web.UI.Page
    {
        protected String PayTypeName;
        protected Int32 PayType;
        protected String UserName;
        protected Int32 Amount;
        protected Int32 Rmb;

        protected Orders Order;
        protected String GotoUrl;
        protected void Page_Load(object sender, EventArgs e)
        {
           if(!IsPostBack)
           {
               var ordReqString = Request.QueryString["j"];
               PayType = Int32.Parse(Request.QueryString["pt"]);
               PayTypeName = Request.QueryString["ptn"].UrlDecode(Encoding.UTF8);

               if(String.IsNullOrEmpty(ordReqString))
               {
                   Page.ClientScript.RegisterClientScriptBlock(GetType(), "", String.Format("<script>alert('参数出错');history.back();</script>"));
                   return;
               }
              
               try
               {
                   var sr = new ShopRepository();
                   Order = sr.GetOrder(long.Parse(ordReqString));
                   var uinfo = new UserInfo();
                   uinfo.GetInfoByUserId(Order.UserId);
                   UserName = uinfo.UserName;
               }
               catch
               {
                   Page.ClientScript.RegisterClientScriptBlock(GetType(), "", String.Format("<script>alert('参数出错');history.back();</script>"));
                   return;
               }
               var ps = new PaymentSetting<YeepayGateway>();
               ps.PayGateway.Merchant.UserName = "10000800500";
               ps.PayGateway.Merchant.Key = "L05KCD6sqv8t52p61l8v9k340l18go6M8955Gno662CRYdAi414Rx5928370";
               ps.PayGateway.Merchant.NotifyUrl = String.Format("http://{0}:{1}/svrs/svr.ashx", Request.ServerVariables["SERVER_NAME"], Request.ServerVariables["SERVER_PORT"]);
               ps.PayGateway.Order.OrderId = "vsacc_" + Order.OrderId;
               ps.PayGateway.Order.Amount = Order.Price;
               ps.PayGateway.Customer.Name = UserName;

               GotoUrl = ps.PayGateway.BuildPaymentUrl();
               
           }
        }
    }
}
